﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileType.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the FileType class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the FileType class.
    /// </summary>
    public static class FileType
    {
        /// <summary>
        /// Initializes a new instance of the FileType class.
        /// </summary>
        /// <param name="fileTypeId">The identity of the database table's row referencing this filetype.</param>
        /// <returns>An instantiated FileType.</returns>
        public static BusinessObjects.FileType Select(int fileTypeId)
        {
            BusinessObjects.FileType fileType = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypeByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FileTypeID", fileTypeId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            fileType = new BusinessObjects.FileType();
                            fileType.Id = fileTypeId;
                            fileType.Extension = rdr["Extension"].ToString();
                            fileType.Mime = rdr["Mime"].ToString();
                        }
                    }
                }
            }

            return fileType;
        }
        
        /// <summary>
        /// Finds a filetype based on its associated file extension, if it exists.
        /// </summary>
        /// <param name="extension">The extension to search for (e.g. "htm")</param>
        /// <returns>If the extension is found, an FileType is populated and returned. Returns null otherwise.</returns>
        public static BusinessObjects.FileType Select(string extension)
        {
            BusinessObjects.FileType fileType = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypeByExtension", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Extension", extension);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            fileType = new BusinessObjects.FileType();
                            fileType.Id = Convert.ToInt32(rdr["FileTypeID"], CultureInfo.InvariantCulture);
                            fileType.Extension = extension;
                            fileType.Mime = rdr["Mime"].ToString();
                        }
                    }
                }
            }

            return fileType;
        }

        /// <summary>
        /// Gets a list of the filetypes associated with this application.
        /// </summary>
        /// <returns>A read only list of HFileType type object.</returns>
        public static List<BusinessObjects.FileType> SelectAll()
        {
            BusinessObjects.FileType fileType = null;
            List<BusinessObjects.FileType> fileTypes = new List<BusinessObjects.FileType>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypes", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fileType = new BusinessObjects.FileType();
                                fileType.Id = Convert.ToInt32(rdr["FileTypeID"], CultureInfo.InvariantCulture);
                                fileType.Mime = rdr["Mime"].ToString();
                                fileType.Extension = rdr["Extension"].ToString();
                                fileTypes.Add(fileType);
                            }
                        }
                    }
                }
            }

            return fileTypes;
        }

        /// <summary>
        /// Inserts the FileType object into the database.
        /// </summary>
        /// <param name="fileType">The FileType to insert.</param>
        /// <returns>The inserted FileType with a newly populated Id field.</returns>
        public static BusinessObjects.FileType Insert(BusinessObjects.FileType fileType)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Extension", fileType.Extension);
                    cmd.Parameters.AddWithValue("@Mime", fileType.Mime);
                    fileType.Id = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return fileType;
        }

        /// <summary>
        /// Updates the FileType's database fields.
        /// </summary>
        /// <param name="fileType">The FileType to update.</param>
        public static void Update(BusinessObjects.FileType fileType)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.UpdateFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FileTypeID", fileType.Id);
                    cmd.Parameters.AddWithValue("@Extension", fileType.Extension);
                    cmd.Parameters.AddWithValue("@Mime", fileType.Mime);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Deletes the FileType from the database.
        /// </summary>
        /// <param name="fileType">The FileType to delete.</param>
        public static void Delete(BusinessObjects.FileType fileType)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FileTypeID", fileType.Id);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}